package com.yz.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.core.exception.JsonException;
import com.yz.core.utils.R;
import com.yz.entity.InvitataionEntity;
import com.yz.entity.UserEntity;
import com.yz.service.ICommentService;
import com.yz.service.IInvitataionService;
import com.yz.service.UserService;
import com.yz.service.impl.LikeService;
import com.yz.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/invitataionController")
public class InvitataionServlet {
    @Autowired
    private IInvitataionService invitataionService;
    @Autowired
    private UserService userService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private LikeService likeService;

    @PostMapping("/page")
    @ResponseBody
    public Page<InvitataionEntity>page(@RequestBody Page<InvitataionEntity> page){
        return invitataionService.page(page);
    }

    @PostMapping("/typeChange")
    @ResponseBody
    public R typeChange(@RequestBody InvitataionEntity invitataionEntity){
        if (invitataionEntity.getType()==1){
            invitataionEntity.setType(2);
        }else {
            invitataionEntity.setType(1);
        }
        invitataionService.updateById(invitataionEntity);
        return R.ok();
    }


    @PostMapping("/publish")
    @ResponseBody
    public R publish(@RequestBody InvitataionEntity invitataionEntity, HttpSession session) throws Exception {

        // 1.获取登录用户
        UserEntity loginUser = (UserEntity) session.getAttribute("loginUser");
        if (loginUser == null) {
            throw new JsonException(100, "录超时，请先登录");
        }
        // 2.获取前端传递的数据
        invitataionEntity.setUserId(loginUser.getId());
        boolean save = invitataionService.save(invitataionEntity);

        return R.out(save);
    }
    @GetMapping("invitataionInfo/{id}")
    @ResponseBody
    public R invitataionInfo(@PathVariable int id){
        InvitataionEntity invitataionEntity = invitataionService.getById(id);
        return R.ok().put("data",invitataionEntity);
    }
    @GetMapping("/info/{id}")
    public String info(@PathVariable Integer id, Model model, HttpSession session) throws Exception {
        // 1.查询帖子
        InvitataionEntity invitataionEntity = invitataionService.getById(id);
        UserEntity loginUser = (UserEntity) session.getAttribute("loginUser");
        // 2.查询作者
        Integer userId = invitataionEntity.getUserId();
        UserEntity userEntity = userService.getById(userId);
        // 3.查询帖子的评论
        List<CommentVo> commentEntityList = commentService.commentListByInvitataionId(loginUser,invitataionEntity.getId());
        Long count = likeService.getInvitataionLikeCount(1, id);//查询帖子点赞数量
        // 获取当前登录用户

        Boolean likeStatus = false;
        if (loginUser != null) {
            // 查看登录用户是否对当前帖子点赞了
            likeStatus = likeService.getInvitataionUserLike(1, loginUser.getId(), id);
        }
        // 4.把帖子的数据和作者的数据放到map中
        Map<String, Object> map = new HashMap<>();
//        map.put("topic", invitataionEntity);
//        map.put("user", userEntity);
//        map.put("commentVoList", commentEntityList);
        model.addAttribute("topic",invitataionEntity);
        model.addAttribute("user", userEntity);
        model.addAttribute("commentVoList", commentEntityList);
        model.addAttribute("likeCount", count); // 帖子的点赞数量
        model.addAttribute("likeStatus", likeStatus ? 1 : 0);
        // 5.跳转到视图
        return "view/site/discuss-detail";
    }
}
